#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

typedef struct
{
	int* a;
	int front;
	int rear;
	int k;
} MyCircularQueue;

bool myCircularQueueIsEmpty(MyCircularQueue* obj)
{
	assert(obj);
	return obj->rear == obj->front;
}

bool myCircularQueueIsFull(MyCircularQueue* obj)
{
	assert(obj);
	return ((obj->rear + 1) % (obj->k + 1) == obj->front);
}
MyCircularQueue* myCircularQueueCreate(int k)
{
	MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
	obj->a = (int*)malloc(sizeof(int) * (k + 1));
	obj->front = obj->rear = 0;
	obj->k = k;

	return obj;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value)
{
	assert(obj);

	if (myCircularQueueIsFull(obj))
	{
		return false;
	}
	obj->a[obj->rear++] = value;
	obj->rear %= (obj->k + 1);

	return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj)
{
	assert(obj);

	if (myCircularQueueIsEmpty(obj))
	{
		return false;
	}
	obj->front++;
	obj->front %= (obj->k + 1);
	return true;
}

int myCircularQueueFront(MyCircularQueue* obj)
{
	assert(obj);
	if (myCircularQueueIsEmpty(obj))
	{
		return -1;
	}
	return obj->a[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj)
{
	assert(obj);
	if (myCircularQueueIsEmpty(obj))
	{
		return -1;
	}
	return obj->a[(obj->rear + obj->k) % (obj->k + 1)];
}

void myCircularQueueFree(MyCircularQueue* obj)
{
	free(obj->a);
	free(obj);
}